package com.aidex.biz.controller;

import java.util.List;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.XiansuoChengben;
import com.aidex.biz.service.XiansuoChengbenService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 线索成本设置Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/biz/bizXiansuoChengben")
public class XiansuoChengbenController extends BaseController {
    @Autowired
    private XiansuoChengbenService xiansuoChengbenService;

    /**
     * 查询线索成本设置列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:list')")
    @GetMapping("/list")
    public R<PageInfo> list(XiansuoChengben xiansuoChengben, HttpServletRequest request, HttpServletResponse response) {
        xiansuoChengben.setPage(new PageDomain(request, response));
        return R.data(xiansuoChengbenService.findPage(xiansuoChengben));
    }

    /**
     * 获取线索成本设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:query')")
    @GetMapping(value = "/{id}")
    public R<XiansuoChengben> detail(@PathVariable("id") String id) {
        return R.data(xiansuoChengbenService.get(id));
    }

    /**
     * 新增线索成本设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:add')")
    @Log(title = "线索成本设置", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated XiansuoChengben xiansuoChengben) {
        return R.status(xiansuoChengbenService.save(xiansuoChengben));
    }

    /**
     * 修改线索成本设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:edit')")
    @Log(title = "线索成本设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated XiansuoChengben xiansuoChengben) {
        return R.status(xiansuoChengbenService.save(xiansuoChengben));
    }


    /**
     * 删除线索成本设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:remove')")
    @Log(title = "线索成本设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(xiansuoChengbenService.deleteXiansuoChengbenByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(xiansuoChengbenService.findMaxSort(new XiansuoChengben()));
    }

    /**
     * 导出线索成本设置列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:export')")
    @Log(title = "线索成本设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(XiansuoChengben xiansuoChengben) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            xiansuoChengben.setCompanyId(null);
        } else {
            xiansuoChengben.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<XiansuoChengben> list = xiansuoChengbenService.findList(xiansuoChengben);
        ExcelUtil<XiansuoChengben> util = new ExcelUtil<XiansuoChengben>(XiansuoChengben.class);
        return util.exportExcel(list, "线索成本设置数据");
    }

    /**
     * 城市+来源唯一性校验
     */
    @PostMapping("/findExist")
    public R findExist(@RequestBody @Validated XiansuoChengben xiansuoChengben) {
        XiansuoChengben exist = xiansuoChengbenService.findExist(xiansuoChengben);
        return R.data(exist);
    }

    /**
     * 批量新增线索成本设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengben:add')")
    @Log(title = "线索成本设置", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public R addBatch(@RequestBody @Validated List<XiansuoChengben> xiansuoChengbenList) {
        return R.status(xiansuoChengbenService.addBatch(xiansuoChengbenList));
    }
}
